home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d14 / burst.arc / BURST.C < prev    next >
Text File  |  1990-09-07  |  6KB  |  173 lines

  1. /***********************************************************************
  2.  * This program was originally written by Michael Harrison [76057,101] *
  3.  * The only problem with the original that I could see was that it was *
  4.  * somewhat of a memory hog.  The burst bitmap that gets popped up on  *
  5.  * the screen was reloaded every time a burst was supposed to pop up.  *
  6.  * Thus, the GDI ended up with about 100 copies of the bitmap (more if *
  7.  * the user let it run after it asked if that was enough).  Even though*
  8.  * this program is kinder to the victim in that it removes itself from *
  9.  * memory after the user terminates it, only the last bitmap was       *
  10.  * removed from the GDI resource pool.  I've cleaned this up a bit.    *
  11.  * Perri Nelson [71401,2116]                                           *
  12.  ***********************************************************************/
  13. /*** Include files needed for definitions, declarations, etc.         ***/
  14. #include <windows.h>  /* Required for all Windows programs              */
  15. #include <stdlib.h>
  16.  
  17. /*** Forward declare functions                                        ***/
  18. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  19. void FAR PASCAL TimerProc(HWND, unsigned, WORD, LONG);
  20.  
  21. HANDLE    hInst;
  22.  
  23.     /* Moved from WndProc to make available to WinMain as well */
  24.     static HANDLE hBitmap;
  25.  
  26.  
  27. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  28.     HANDLE      hInstance, hPrevInstance;
  29.     LPSTR       lpszCmdLine;
  30.     int         nCmdShow;
  31.     {
  32.     HWND        hWnd;
  33.     MSG         msg;
  34.     WNDCLASS    wndclass;
  35.     FARPROC     lpfnTimerProc;
  36.  
  37.     if (!hPrevInstance)
  38.         {
  39.         wndclass.style          = CS_HREDRAW;
  40.         wndclass.lpfnWndProc    = WndProc;
  41.         wndclass.cbClsExtra     = 0;
  42.         wndclass.cbWndExtra     = 0;
  43.         wndclass.hInstance      = hInstance;
  44.         wndclass.hIcon          = NULL;
  45.         wndclass.hCursor        = LoadCursor (hInstance, IDC_ARROW);
  46.         wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  47.         wndclass.lpszMenuName   = NULL;
  48.         wndclass.lpszClassName  = "Burst";
  49.  
  50.     if (!RegisterClass (&wndclass) )
  51.         return FALSE;
  52.         }
  53.     else
  54.         return FALSE;
  55.  
  56.     hInst=hInstance;
  57.     hWnd = CreateWindow ("Burst", "Burst", WS_OVERLAPPEDWINDOW, 0, 0,
  58.                         0,0, NULL, NULL, hInstance, NULL);
  59.  
  60.     lpfnTimerProc = MakeProcInstance( TimerProc, hInstance );
  61.  
  62.     if(!SetTimer(hWnd, 1, 60000, lpfnTimerProc))
  63.         {
  64.         MessageBox(hWnd, "Too many clocks or timers!", "Burst",
  65.                    MB_ICONEXCLAMATION | MB_OK);
  66.         return FALSE;
  67.         }
  68.                 /* Moved from WndProc to make more memory efficient */
  69.                 hBitmap=LoadBitmap(hInst, "Burst");
  70.  
  71.     while (GetMessage(&msg, NULL, 0,0 ))
  72.         {
  73.         TranslateMessage(&msg);
  74.         DispatchMessage(&msg);
  75.         }
  76.     return msg.wParam;
  77.     }
  78.  
  79. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  80.     HWND          hWnd;
  81.     unsigned      iMessage;
  82.     WORD          wParam;
  83.     LONG          lParam;
  84.     {
  85.     BITMAP        bm;
  86.     HDC           hDC,hMemDC;
  87.     short         nRand;
  88.     long          xStart, yStart;
  89.     static char   cJunk=0;
  90.     unsigned int  nIndex;
  91.     static int    nNoise=0;
  92.  
  93.  
  94.     switch (iMessage)
  95.         {
  96.         case WM_TIMER:
  97.             if(rand() < 2000)
  98.                 {
  99.  
  100.                 GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &bm);
  101.  
  102.                 hDC=CreateDC("DISPLAY", NULL, NULL, NULL);
  103.                 hMemDC=CreateCompatibleDC(hDC);
  104.                 SelectObject(hMemDC, hBitmap);
  105.  
  106.                 xStart=rand() % GetSystemMetrics(SM_CXSCREEN);
  107.                 yStart=rand() % GetSystemMetrics(SM_CYSCREEN);
  108.                 BitBlt(hDC, (short)xStart, (short)yStart, bm.bmWidth, bm.bmHeight,
  109.                        hMemDC, 0,0, SRCAND);
  110.  
  111.                 DeleteDC(hDC);
  112.                 DeleteDC(hMemDC);
  113.  
  114.                 OpenSound();
  115.                 StopSound();
  116.                 SetSoundNoise(nNoise++, 100);
  117.                 StartSound();
  118.                 for (nIndex=0; nIndex < 65535; nIndex++); /* pause      */
  119.                 StopSound();
  120.  
  121.                 if ( cJunk == 100 ) {
  122.                     cJunk = 0;
  123.                     KillTimer( hWnd, 1 );
  124.                     if ( MessageBox( hWnd, "Had enough?", "Burst by MSH",MB_YESNO | MB_ICONQUESTION ) == IDYES )
  125.                         SendMessage( hWnd, WM_CLOSE, 0, 0L );
  126.                     else
  127.                         if(!SetTimer(hWnd, 1, 100, NULL))
  128.                         {
  129.                             MessageBox(hWnd, "Too many clocks or timers!", "Error",
  130.                             MB_ICONEXCLAMATION | MB_OK);
  131.                         }
  132.                 }
  133.                 else
  134.                     cJunk++;
  135.                 }
  136.  
  137.             break;
  138.  
  139.         case WM_CREATE:
  140.             OpenSound();
  141.             srand((unsigned)GetTickCount());
  142.             break;
  143.  
  144.         case WM_DESTROY:
  145.             DeleteObject(hBitmap);
  146.             PostQuitMessage(0);
  147.             CloseSound();
  148.             StopSound();
  149.             KillTimer( hWnd, 1 );
  150.             break;
  151.  
  152.         default:
  153.             return (DefWindowProc(hWnd, iMessage, wParam, lParam));
  154.         }
  155.     return 0L;
  156. }
  157.  
  158.  
  159. void FAR PASCAL TimerProc( hWnd, iMessage, wParam, lParam )
  160.     HWND hWnd;
  161.     unsigned iMessage;
  162.     WORD wParam;
  163.     long lParam;
  164. {
  165.     KillTimer( hWnd, 1 );
  166.     if(!SetTimer(hWnd, 1, 50, NULL))
  167.     {
  168.         MessageBox(hWnd, "Too many clocks or timers!", "Error",
  169.            MB_ICONEXCLAMATION | MB_OK);
  170.         SendMessage( hWnd, WM_CLOSE, 0, 0L );
  171.     }
  172. }
  173.